package com.enqualcomm.kids.view.fencing;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.enqualcomm.kids.R;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by yinzhiqun on 2015/12/24.
 *
 */
public class FencingTypeView extends View implements IFencingTypeView {


    public FencingTypeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public FencingTypeView(Context context) {
        super(context);
    }

    public FencingTypeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    int fencingType;

    @Override
    public Observable<Integer> getType() {
        return RxView.clicks(this).throttleFirst(1000, TimeUnit.MICROSECONDS).map(new Func1<Void, Integer>() {
            @Override
            public Integer call(Void aVoid) {
                if (fencingType==TYPE_CIECLE){
                    fencingType = TYPE_RECT;
                    setBackgroundResource(R.drawable.change1);
                }else{
                    fencingType = TYPE_CIECLE;
                    setBackgroundResource(R.drawable.change2);
                }

                return fencingType;
            }
        });
    }

    @Override
    public void setType(int type) {
        fencingType = type;
        if (type==TYPE_CIECLE){
            setBackgroundResource(R.drawable.change2);
        }else{
            setBackgroundResource(R.drawable.change1);
        }
    }
}
